package com.push.api.db;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.core.db.dialect.DBUtil;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.push.api.vo.AppConfig;
import com.push.api.vo.Offer;
import com.push.api.vo.PushLog;

public class DbUtil {

    private static SqlMapClient smc = null;
    
    static {
        if (smc == null) {
            Reader rd;
            try {
                rd = Resources.getResourceAsReader("SqlMapConfig.xml");
                smc = SqlMapClientBuilder.buildSqlMapClient(rd);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public static List<Offer> getOffers(){
     
        List<Offer> list = new ArrayList<Offer>();
        
        try {
            list = (List<Offer>) smc.queryForList("total.SelectActiveOffer");
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return list;
    }
    
    public static AppConfig getAppconfig(String packagename) {
        AppConfig appconfig = null;
        
        try {
            appconfig = (AppConfig) smc.queryForObject("total.SelectConfigByApp", packagename);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return appconfig;
    }
    
    public static void insertNewAppConfig(String packagename) {
        try {
            smc.insert("total.InsertAppConfig", packagename);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void insertPushLog (PushLog log) {
        try {
            smc.insert("total.InsertLog", log);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
    static Logger logger = LoggerFactory.getLogger("ip_track_log"); 
    
    public static void main(String[] args) {
        logger.error("sdd");
//        System.out.println(DbUtil.getOffers().get(0).getName());
//        System.out.println(DbUtil.getAppconfig("test11"));
////        DbUtil.insertNewAppConfig("test1");
//        PushLog log = new PushLog();
//        log.setApp_packagename("sdsd");
//        log.setOfferid(1);
//        log.setDevice("dsdsd");
//        log.setOffer_packagename("test1");
//        log.setType("1");
//        DbUtil.insertPushLog(log);
    }
}
